<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: vertx/multi_map.js</title>
    
    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">
    
    <h1 class="page-title">Source: vertx/multi_map.js</h1>
    
    


    
    <section>
        <article>
            <pre class="prettyprint source"><code>/*
 * Copyright 2011-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

if (typeof __vertxload === 'string') {
  throw "Use require() to load Vert.x API modules"
}

/**
 * A &lt;code>MultiMap&lt;/code> is a type of Map object that can store more
 * than one value for a given key. This is useful for storing and passing
 * around things like HTTP headers where a single header can have multiple
 * values.
 *
 * @constructor
 * @param {org.vertx.java.core.MultiMap} multiMap the underlying Java MultiMap instance
 */
MultiMap = function(j_map) {

  /**
   * Return the value for the given name
   *
   * @param {string} name The name to lookup in the map
   * @returns {string} value The value for the given name. If more than one value maps to 
   *          &lt;code>name&lt;/code>, the first value is returned.
   */
  this.get = function(name) {
    var value = j_map.get(name);
    // Handles discrepancy between how rhino and dynjs deal with null return values 
    // from Java objects. It seems as though Rhino changes the null to undefined, or
    // perhaps has some distinction where undefined is returned iff the key isn't there.
    if (value == null) { 
      return undefined 
    }
    return j_map.get(name);
  }

  /**
   * Execute the given function for every name, value pair stored
   *
   * @param {function} func The function to execute
   */
  this.forEach = function(func) {
    var names = j_map.names().iterator();
    while (names.hasNext()) {
      var name = names.next();
      var values = j_map.getAll(name).iterator();
      while (values.hasNext()) {
        func(name, values.next());
      }
    }
  }

  /**
   * Return all values stored for the given name.
   *
   * @param {string} name The name to lookup values for
   * @returns {Array} The values for the given name
   */
  this.getAll = function(name) {
    var n =  j_map.getAll(name);
    return _convertToArray(n);
  }

  /**
   * Returns if a value for the given name is stored
   *
   * @param {string} name The name to check for
   * @returns {boolean} &lt;code>true&lt;/code> if &lt;code>name&lt;/code> is stored in this map
   */
  this.contains = function(name) {
    return j_map.contains(name);
  }

  /**
   * Returns if this map is empty
   *
   * @returns {boolean} &lt;code>true&lt;/code> if empty
   */
  this.isEmpty = function() {
    return j_map.isEmpty();
  }

  /**
   * Return all names for which values are stored
   *
   * @returns {Array} The names for which values are stored
   */
  this.names = function() {
    var n =  j_map.names();
    return _convertToArray(n);
  }

  /**
   * Add a value for the given name
   *
   * @param {string} name The name under which the value should be stored
   * @param {string} value The value to store
   * @returns {module:vertx/multi_map~MultiMap}
   */
  this.add = function(name, value) {
    j_map.add(name, value);
    return this;
  }

  /**
   * Set a value for the given name. All previous stored values under the name will get deleted.
   *
   * @param {string} name The name under which the value should be stored
   * @param {string} value The value to store
   * @returns {module:vertx/multi_map~MultiMap}
   */
  this.set = function(name, value) {
    j_map.set(name, value);
    return this;
  }

  /**
   * Set the content of the given map.
   *
   * @param {module:vertx/multi_map~MultiMap} The map to set
   * @returns {module:vertx/multi_map~MultiMap} self
   */
  this.setMap = function(map) {
    j_map.set(map._jmap);
    return this;
  }

  /**
   * Remove all values stored under the name
   *
   * @param {string} name The name for which all values should be removed
   * @returns {module:vertx/multi_map~MultiMap} self
   */
  this.remove = function(name) {
    j_map.remove(name);
    return this;
  }

  /**
   * Clears the map
   *
   * @returns {module:vertx/multi_map~MultiMap} self
   */
  this.clear = function() {
    j_map.clear();
    return this;
  }

  /**
   * Return the number of names stored.
   * @returns {number} the number of names stored
   */
  this.size = function() {
    return j_map.size();
  }

  /**
   * @private
   */
  this._jmap = j_map;
}

function _convertToArray(j_col) {
  var n = j_col.iterator();
  var array = new Array();
  var i = 0;

  while (n.hasNext()) {
    array[i++] = n.next();
  }
  return array;
}

/**
 * @module vertx/multi_map
 */
module.exports.MultiMap = MultiMap;
</code></pre>
        </article>
    </section>




</div>

<nav>
    <h2><a href="index.html">Index</a></h2><h3>Modules</h3><ul><li><a href="module-vertx.html">vertx</a></li><li><a href="buffer.html">vertx/buffer</a></li><li><a href="console.html">vertx/console</a></li><li><a href="container.html">vertx/container</a></li><li><a href="event_bus.html">vertx/event_bus</a></li><li><a href="file_system.html">vertx/file_system</a></li><li><a href="http.html">vertx/http</a></li><li><a href="multi_map.html">vertx/multi_map</a></li><li><a href="net.html">vertx/net</a></li><li><a href="parse_tools.html">vertx/parse_tools</a></li><li><a href="pump.html">vertx/pump</a></li><li><a href="shared_data.html">vertx/shared_data</a></li><li><a href="sockjs.html">vertx/sockjs</a></li><li><a href="timer.html">vertx/timer</a></li></ul><h3>Externals</h3><ul><li><a href="InetSocketAddress.html">InetSocketAddress</a></li><li><a href="Buffer_.html">Buffer</a></li><li><a href="RecordParser.html">RecordParser</a></li><li><a href="SockJSSocket.html">SockJSSocket</a></li></ul><h3>Classes</h3><ul><li><a href="buffer-Buffer.html">Buffer</a></li><li><a href="file_system.AsyncFile.html">AsyncFile</a></li><li><a href="http.HttpClient.html">HttpClient</a></li><li><a href="http.HttpClientRequest.html">HttpClientRequest</a></li><li><a href="http.HttpClientResponse.html">HttpClientResponse</a></li><li><a href="http.HttpServer.html">HttpServer</a></li><li><a href="http.HttpServerFileUpload.html">HttpServerFileUpload</a></li><li><a href="http.HttpServerRequest.html">HttpServerRequest</a></li><li><a href="http.HttpServerResponse.html">HttpServerResponse</a></li><li><a href="http.RouteMatcher.html">RouteMatcher</a></li><li><a href="http.WebSocket.html">WebSocket</a></li><li><a href="multi_map-MultiMap.html">MultiMap</a></li><li><a href="net.NetClient.html">NetClient</a></li><li><a href="net.NetServer.html">NetServer</a></li><li><a href="net.NetSocket.html">NetSocket</a></li><li><a href="pump-Pump.html">Pump</a></li><li><a href="sockjs.SockJSServer.html">SockJSServer</a></li></ul><h3>Mixins</h3><ul><li><a href="ClientSSLSupport.html">ClientSSLSupport</a></li><li><a href="ReadStream.html">ReadStream</a></li><li><a href="ServerSSLSupport.html">ServerSSLSupport</a></li><li><a href="ServerTCPSupport.html">ServerTCPSupport</a></li><li><a href="SSLSupport.html">SSLSupport</a></li><li><a href="TCPSupport.html">TCPSupport</a></li><li><a href="WriteStream.html">WriteStream</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>

<br clear="both">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.0-dev</a> on Wed Jul 17 2013 20:27:22 GMT+0100 (BST)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>